package com.example.springbootdemo.api.controller;

import com.example.springbootdemo.app.service.CourseService;
import com.example.springbootdemo.domin.entity.Course;
import com.example.springbootdemo.infra.util.CourseException;
import com.example.springbootdemo.infra.util.Result;

import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@RestController
@RequestMapping("/course")
@Api(value = "课程相关controller", tags = {"课程相关接口"})
public class CourseController {
    @Autowired
    private CourseService courseService;
    private final Logger logger = LoggerFactory.getLogger(CourseController.class);

    /**
     * 根据课程号或者课程名或者老师名来查询课程信息
     */
    @PostMapping("/getCourse")
    @ApiOperation(value = "根据课程号或者课程名来查询课程成绩")
    public Result<Course> getCourse(@RequestBody Course course) {
        logger.info("课程号与课程名为:{}", course);
        Course coursedetail = courseService.getCourse(course);
        return Result.success(coursedetail);
    }

    /**
     * 根据课程号来查询
     */
    @GetMapping("/getById/{cno}")
    @ApiOperation(value = "根据课程号查询课程相关信息")
    public ModelAndView getById(@PathVariable Integer cno) {
        if (cno == null) {
            //throw new Exception();todo
        }
        logger.info("课程号为:{}", cno);
        Course course = new Course();
        course.setCno(cno);
        Course courseById = courseService.getCourse(course);
        ModelAndView modelAndView = new ModelAndView("change_course");
        modelAndView.addObject("course", courseById);
        return modelAndView;
    }

    /**
     * 增加课程信息
     */
    @PostMapping("/addCourse")
    @ApiOperation(value = "添加课程")
    public ModelAndView addCourse(Course course) {
        logger.info("要添加的课程为:{}", course);
        courseService.addCourse(course);
        List<Course> all = courseService.getAll();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("course");
        modelAndView.addObject("cs", all);
        return modelAndView;
    }

    /**
     * 修改课程信息(课程名或者老师编号)
     */
    @PostMapping("/updateCourse")
    @ApiOperation(value = "修改课程信息")
    public ModelAndView updateCourse(Course course) {
        logger.info("要修改的课程为:{}", course);
        courseService.updateCourse(course);
        List<Course> all = courseService.getAll();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("course");
        modelAndView.addObject("cs", all);
        return modelAndView;
    }

    /**
     * 删除课程信息
     */
    @GetMapping("/deleteCourse/{cno}")
    @ApiOperation(value = "删除课程")
    public ModelAndView deleteCourse(@PathVariable("cno") Integer cno) {
        if (cno == null) {
            //throw new Exception();todo
        }
        logger.info("要删除的课程号为:{}", cno);
        List<Course> all = courseService.getAll();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("course");
        modelAndView.addObject("cs", all);
        courseService.deleteCourse(cno);
        return modelAndView;
    }

    @GetMapping("/getAll")
    @ApiOperation(value = "返回课程列表")
    public ModelAndView getAll() {
        List<Course> all = courseService.getAll();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("course");
        modelAndView.addObject("cs", all);
        return modelAndView;
    }
}
